package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IContentService;
import com.mall.shopping.IHomeService;
import com.mall.shopping.IItemCategoriesService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/18 16:42
 */

@Slf4j
@RestController
@RequestMapping("/shopping")
public class ShoppingController {

    @Reference(interfaceClass = IHomeService.class, timeout = 3000, retries = 0, check = false)
    IHomeService iHomeService;

    @Reference(interfaceClass = IContentService.class, timeout = 3000, retries = 0, check = false)
    IContentService iContentService;

    @Reference(interfaceClass = IItemCategoriesService.class, timeout = 3000, retries = 0, check = false)
    IItemCategoriesService iItemCategoriesService;

    @Reference(interfaceClass = IProductService.class, timeout = 3000, retries = 0, check = false)
    IProductService iProductService;


    /**
     * 获取首页商品信息
     *
     * @return
     */
    @GetMapping("/homepage")
    @Anoymous
    public ResponseData shoppingHomepage() {
        //获取首页的商品信息
        HomePageResponse homePageResponse = iHomeService.homepage();
        if (ShoppingRetCode.SUCCESS.getCode().equals(homePageResponse.getCode())) {
            return new ResponseUtil().setData(homePageResponse.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(homePageResponse.getMsg());
    }


    /**
     * 获取导航栏信息
     *
     * @return
     */
    @GetMapping("/navigation")
    @Anoymous
    public ResponseData shoppingNavigation() {

        NavListResponse navListResponse = iContentService.queryNavList();
        if (ShoppingRetCode.SUCCESS.getCode().equals(navListResponse.getCode())) {
            return new ResponseUtil().setData(navListResponse.getPannelContentDtos());
        }
        return new ResponseUtil().setErrorMsg(navListResponse.getMsg());
    }


    /**
     * 获取商品类目信息
     *
     * @return
     */
    @GetMapping("/categories")
    @Anoymous
    public ResponseData shoppingCategories() {
        AllProductCateResponse allProductCateResponse = iItemCategoriesService.queryItemCategories();
        if (ShoppingRetCode.SUCCESS.getCode().equals(allProductCateResponse.getCode())) {
            return new ResponseUtil().setData(allProductCateResponse.getProductCateDtoList());
        }
        return new ResponseUtil().setErrorMsg(allProductCateResponse.getMsg());
    }


    /**
     * 热门商品推荐
     *
     * @return
     */
    @GetMapping("/recommend")
    @Anoymous
    public ResponseData shoppingRecommend() {
        //获取首页热门商品信息
        HomePageResponse homePageResponse = iHomeService.recommendHomepage();
        if (ShoppingRetCode.SUCCESS.getCode().equals(homePageResponse.getCode())) {
            return new ResponseUtil().setData(homePageResponse.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(homePageResponse.getMsg());
    }


    /**
     * 分页查询商品列表
     *
     * @return
     */
    @GetMapping("/goods")
    @Anoymous
    // Integer page,Integer size,String sortnteger priceGt,Integer priceLte
    public ResponseData shoppingGoods(Integer page, Integer size, String sort, Integer priceGt, Integer priceLte) {
        AllProductRequest allProductRequest = new AllProductRequest(page, size, sort, null, priceGt, priceLte);
        AllProductResponse allProductResponse = iProductService.getAllProduct(allProductRequest);
        AllProductResponse1 allProductResponse1 = new AllProductResponse1();
        allProductResponse1.setData(allProductResponse.getProductDtoList());
        allProductResponse1.setTotal(allProductResponse.getTotal());
        if (ShoppingRetCode.SUCCESS.getCode().equals(allProductResponse.getCode())) {
            return new ResponseUtil().setData(allProductResponse1);
        }
        return new ResponseUtil().setErrorMsg(allProductResponse.getMsg());
    }


    /**
     * 查看商品详情
     *
     * @return
     */
    @GetMapping("/product/{id}")
    @Anoymous
    public ResponseData shoppingProduct(@PathVariable("id") Long id) {
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(id);
        ProductDetailResponse productDetailResponse = iProductService.getProductDetail(productDetailRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetailResponse.getCode())) {
            return new ResponseUtil().setData(productDetailResponse.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(productDetailResponse.getMsg());
    }


    @GetMapping("comment/count")
    public ResponseData getComment(Integer productId) {
        return new ResponseUtil<>().setErrorMsg("没评论,不想听你们逼逼赖赖");
    }


}
